﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTGroup.Networking.Framework.Utils;

namespace DTGroup.iNek.WinUI._Codes
{
    public class GeoCoordSysManager : DTGroup.Networking.Framework.Singletonable<GeoCoordSysManager>
    {
        /// <summary>
        /// 锁
        /// </summary>
        private object locker = new object();

        /// <summary>
        /// 系统地图实体
        /// </summary>
        private List<GeoCoordSysEntity> geoCoordSysCollection = null;

        public List<GeoCoordSysEntity> GeoCoordSysCollection
        {
            get
            {
                lock (locker)
                {
                    if (geoCoordSysCollection == null)
                    {

                        geoCoordSysCollection = GeoCoordSysManager.Instance.GetAll();

                        if (geoCoordSysCollection == null)
                        {
                            geoCoordSysCollection = new List<GeoCoordSysEntity>();
                        }
                    }

                    return geoCoordSysCollection;
                }
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        private List<GeoCoordSysEntity> GetAll()
        {
            List<GeoCoordSysEntity> geoCoordSyss = new List<GeoCoordSysEntity>();

            Type type = typeof(SuperMap.Data.GeoCoordSysType);
            System.Reflection.FieldInfo[] fieldInfos = type.GetFields();

            int cursor = 0;
            foreach (System.Reflection.FieldInfo item in fieldInfos)
            {
                SuperMap.Data.GeoCoordSysType geoCoordSysType = StringUtil.TryParse<SuperMap.Data.GeoCoordSysType>(item.Name);
                if (item.Name.IndexOf("value__") == -1)
                {
                    using (SuperMap.Data.GeoCoordSys geoCoordSys = new SuperMap.Data.GeoCoordSys(geoCoordSysType, SuperMap.Data.GeoSpatialRefType.EarthLongitudeLatitude))
                    {
                        geoCoordSyss.Add(new GeoCoordSysEntity()
                        {
                            OID = cursor,
                            CoordUnit = geoCoordSys.CoordUnit.ToString(),
                            GeoDatum = geoCoordSys.GeoDatum.Name,
                            Name = item.Name,
                            GeoPrimeMeridian = geoCoordSys.GeoPrimeMeridian.Name,
                            Type = geoCoordSys.Type.ToString()
                        });
                    }
                };

                cursor++;
            }

            return geoCoordSyss;
        }

        public void ClearMemory()
        {
            lock (locker)
            {
                geoCoordSysCollection = null;
            }
        }
    }
}
